<?php

App::uses('AppController', 'Controller');

class PagesController extends AppController {

	public $uses = array('Page', 'Group');

	public function home() {
	    
    	$homeGroups = $this->Group->find('all', array(
    	    'fields'     => array('Group.id', 'Group.name', 'Group.key'),
	        'conditions' => array( 'Group.home' => 1 ),
    	    'recursive'  => 0
	    ));
    	if (! empty($homeGroups)) {
    	    foreach ($homeGroups as &$group) {
    	        
    	        $products = $this->Group->Product->find('all', array(
    	            'fields'     => array('Product.*'),
    	            'conditions' => array('Product.group_id' => $group['Group']['id']),
    	            'limit'      => 4,
	                'order'      => 'RAND()'
    	        ));
    	        $group['Product'] = $products ? $products : array();
    	    }
    	}
	    
    	$this->set(compact('homeGroups'));
    }

	public function display($id) {
        $page = $this->Page->findById((int)$id);
        if (! $page) {
            $this->redirect('/');
        }
		
		$this->pageTitle = $page['Page']['menu_label'];
		$this->set('page', $page);
	}
}
